package com.fenghongzhang.day019;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MusicUtil {
    //播放模式的常量值
    public static final int ORDER = 0;
    public static final int SINGLE = 1;
    public static final int RANDOM = 2;

    public static List<Music> getMusics(Context context){
        List<Music> list = new ArrayList<>();
        //
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(query != null){
            while (query.moveToNext()){
                String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duration = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
                String albumId = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                if (Integer.parseInt(duration) > 60*1000){
                    Music music = new Music(title, artist, duration, data, size, albumId);
                    list.add(music);
                }
            }
            query.close();
        }
        return list;
    }

    //格式化时间
    public static String fmtTime(String time){ //3:15 3:09
        int i = Integer.parseInt(time);
        if(i /1000 % 60 < 10){
            return i / 1000 /60 +":0"+ i /1000 % 60;
        }
        return i / 1000 /60 +":"+ i /1000 % 60;
    }


}
